package edu.ncsu.csc.ip.routing.ls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Class <code>Node</code>
 * 
 * Represents Graph Node
 * 
 * @author Moin Ayazifar <mayazif@ncsu.edu>
 * @author Steven Elliott <sdelliot@ncsu.edu>
 * 
 */

public final class Node implements Comparable<Node> {

	//node's name
	private final String name;

	//list of neighbors
	private List<Edge> neighbours = new ArrayList<Edge>();
	
	private Map<Node,Edge> neighboursMap = new HashMap<Node,Edge>();

	//minimum cost to source specified in LinkState
	private double minCost = LinkState.INFINITY;

	//previous node on the shortest path to this node
	public Node previous;

	public Node(String name) {
		this.name = name;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	public void addNeighbour(Edge edge) {
		this.neighbours.add(edge);
		this.neighboursMap.put(edge.getDestination(), edge);
	}

	@Override
	public String toString() {
		return getName();
	}

	@Override
	public int compareTo(Node node) {
		return Double.compare(getMinCost(), node.getMinCost());
	}
	
	/**
	 * @return the neighbours
	 */
	public List<Edge> getNeighbours() {
		return neighbours;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object node) {
		if (this == node)
			return true;
		if (node == null)
			return false;
		if (getClass() != node.getClass())
			return false;
		Node otherNode = (Node) node;
		if (name == null) {
			if (otherNode.name != null)
				return false;
		} else if (!name.equals(otherNode.name))
			return false;
		return true;
	}

	/**
	 * @return the minCost
	 */
	public double getMinCost() {
		return minCost;
	}

	/**
	 * @param minCost the minCost to set
	 */
	public void setMinCost(double minCost) {
		this.minCost = minCost;
	}

	
	public Map<Node,Edge> getNeighboursMap() {
		return neighboursMap;
	}

	

}
